<?php

namespace app\admin\controller;

use app\common\BaseHelper;
use think\Request;

/**
 * 部门
 * Class Department
 * @package app\admin\controller
 */
class Department extends Base
{

    public function _initialize() {
        parent::_initialize();
        $this->searchFields = [
            'name' => [
                'label'     => '部门名称',
                'field'     => 'name',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];
        $this->redirect = 'admin/department/index';
        $this->defaultOrder = 'sorting ASC';
    }

    public function index(){
        return parent::index();
    }

    public function add(){
        return $this->fetch();
    }

    public function addPost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            $id = BaseHelper::getUUID();
            $model = new \app\admin\model\Department();
            $model->id = $id;
            $model->name = $param['name'];
            $model->imgUrl = $param['imgUrl'];
            $model->address = $param['address'];
            $model->phone = $param['phone'];
            $model->sorting = $param['sorting']?$param['sorting']:0;
            $model->introduce = $param['brief'];
            if($model->save()){
                return $this->success('添加成功',$this->redirect);
            }
        }else{
            return $this->error('非法请求');
        }
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }
        $Department = \app\admin\model\Department::get($param['id']);
        return $this->fetch('',[
            'data' => $Department
        ]);
    }

    public function editPost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(empty($param['id'])){
                return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
            }
            $Department = \app\admin\model\Department::get($param['id']);
            $Department->name = $param['name'];
            $Department->address = $param['address'];
            $Department->imgUrl = $param['imgUrl'];
            $Department->phone = $param['phone'];
            $Department->sorting = $param['sorting'];
            $Department->introduce = $param['brief'];

            $result = $Department->save();
            if($result !== false){
                return $this->success('修改成功',$this->redirect);
            }else{
                return $this->success('修改失败',$this->redirect);
            }
        }else{
            return $this->error('error','admin/' . strtolower($this->model) . '/index');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        return $where;
    }
}